from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QHBoxLayout, QVBoxLayout, QLabel, QPushButton
from PyQt5.QtGui import QIcon
import sys


def calculate_percentage(a, b):
    if b == 0:
        return "错误：分母不能为0"
    else:
        percentage = (a / b) * 100
        rounded_percentage = round(percentage * 100) / 100
        return rounded_percentage


# noinspection PyUnresolvedReferences
class MyWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.totalNum = 0
        self.tNum = 0

        self.widget = QWidget()
        self.mainLayout = QVBoxLayout()

        self.displaytxt = QLabel('')
        self.displaytxt.setStyleSheet('font-family:抖音美好体;font-size:40px;color:#C8C8C8')
        self.mainLayout.addWidget(self.displaytxt)

        self.childLayout = QHBoxLayout()
        self.T = QPushButton('+1')
        self.T.setStyleSheet('font-family:抖音美好体;color:#C8C8C8')
        self.childLayout.addWidget(self.T)

        self.F = QPushButton('0')
        self.F.setStyleSheet('font-family:抖音美好体;color:#C8C8C8')
        self.childLayout.addWidget(self.F)

        self.mainLayout.addLayout(self.childLayout)

        self.cl = QPushButton('清除')
        self.cl.setStyleSheet('font-family:抖音美好体;color:#C8C8C8')
        self.mainLayout.addWidget(self.cl)

        self.title = QLabel('辉月制作，不喜勿喷！')
        self.title.setStyleSheet('font-family:抖音美好体;font-size:20px;color:#C8C8C8')
        self.mainLayout.addWidget(self.title)

        self.setWindowTitle('[辉月]增量准确度测试器')
        self.setWindowIcon(QIcon("./assets/icon.ico"))

        self.widget.setLayout(self.mainLayout)
        self.setCentralWidget(self.widget)

        self.T.clicked.connect(self.TClicked)
        self.F.clicked.connect(self.FClicked)
        self.cl.clicked.connect(self.clearEvent)

    def TClicked(self):
        self.totalNum += 1
        self.tNum += 1

        self.updateEvent()

    def FClicked(self):
        self.totalNum += 1

        self.updateEvent()

    def updateEvent(self):
        self.displaytxt.setText(f'{self.tNum}/{self.totalNum} 准度约为{calculate_percentage(self.tNum, self.totalNum)}％')

    def clearEvent(self):
        self.displaytxt.setText('')
        self.totalNum = 0
        self.tNum = 0
